package com.project.internship.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.internship.entity.Allot;
import com.project.internship.entity.Student;
import com.project.internship.entity.Teacher;
import com.project.internship.mapper.AllotMapper;
import com.project.internship.service.AllotService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.internship.utils.UserUtils;
import com.project.internship.utils.YearUtils;
import com.project.internship.vo.StudentDeptMajorClassVO;
import com.project.internship.vo.StudentRecordVO;
import com.project.internship.vo.TeacherDeptVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2020-04-11
 */
@Service
public class AllotServiceImpl extends ServiceImpl<AllotMapper, Allot> implements AllotService {

    @Autowired
    AllotMapper allotMapper;

    /**
     * 教师端查询学生信息
     * 根据学号
     * @param page
     * @param keyword
     * @return
     */
    @Override
    public IPage<StudentDeptMajorClassVO> getAllAllotStudents(Page<StudentDeptMajorClassVO> page, String keyword) {
        return allotMapper.getAllAllotStudents(page, UserUtils.getCurrentUser().getTeacherId(),keyword);
    }

    @Override
    public IPage<StudentDeptMajorClassVO> getAllReceivedStudents(Page<StudentDeptMajorClassVO> page, String keyword) {
        return allotMapper.getAllReceivedStudents(page, UserUtils.getCurrentUser().getBaseId(),keyword);
    }

    @Override
    public int addAllot(Integer bid, Integer[] tids, Integer[] sids) {
        Allot allot = new Allot();
        int num = 0;

        allot.setBid(bid);
        if(tids.length != 0 && sids.length != 0) {
            for (Integer tid : tids) {
                allot.setTid(tid);
                for (Integer sid : sids) {
                    allot.setSid(sid);
                    allotMapper.insert(allot);
                    num++;
                }
            }
            return num;
        }
        return 0;
    }

    @Override
    @Transactional
    public Integer updateAllot(Integer bid, Integer[] tids, Integer[] sids) {
        //查询allot表中bid为传入的值，创建时间为今年yyyy 的id值
        QueryWrapper<Allot> wrapper = new QueryWrapper<>();
        wrapper.select("id")
                .eq("bid", bid)
                .likeRight("create_time", YearUtils.getCurrentYear());
        List<Allot> allots = allotMapper.selectList(wrapper);
        List<Integer> ids = new ArrayList<>();
        for (Allot allot : allots) {
            ids.add(allot.getId());
        }
        //删除id为ids中值得记录  即删除所有bid为传入的值，创建时间为今年yyyy的记录
        allotMapper.deleteBatchIds(ids);

        //重新添加
        Allot allot = new Allot();
        Integer num = 0;
        allot.setBid(bid);
        for (Integer tid : tids) {
            allot.setTid(tid);
            for (Integer sid : sids) {
                allot.setSid(sid);
                allotMapper.insert(allot);
                num++;
            }
        }
        return num;
    }

    @Override
    public Integer deleteAllot(Integer bid) {
        //查询allot表中bid为传入的值，创建时间为今年yyyy 的id值
        QueryWrapper<Allot> wrapper = new QueryWrapper<>();
        wrapper.select("id")
                .eq("bid", bid)
                .likeRight("create_time", YearUtils.getCurrentYear());
        List<Allot> allots = allotMapper.selectList(wrapper);
        List<Integer> ids = new ArrayList<>();
        for (Allot allot : allots) {
            ids.add(allot.getId());
        }
        //删除id为ids中值的记录  即删除所有bid为传入值且创建时间为今年yyyy的记录
        //返回删除的记录数
        return allotMapper.deleteBatchIds(ids);
    }

    @Override
    public IPage<StudentDeptMajorClassVO> getBaseStudent(Page<StudentDeptMajorClassVO> page, Integer bid) {
        return allotMapper.getBaseStudent(page, bid, UserUtils.getCurrentUser().getDepartmentId(), YearUtils.getCurrentYear());
    }

    @Override
    public IPage<StudentRecordVO> getStudentRecord(Page<StudentRecordVO> page, String keyword) {
        return allotMapper.getStudentRecord(page,UserUtils.getCurrentUser().getDepartmentId(),keyword);
    }
}
